package vn.gq.udv.comic;

import vn.gq.udv.utils.Client;
import vn.gq.udv.utils.NetworkOption;
import vn.gq.udv.utils.Client.DataType;
import vn.gq.udv.utils.UrlHandler;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


public class HomeActivity extends BaseActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home);
		
		((TextView) findViewById(R.id.tv_marquee_notification)).setSelected(true);
		((Button) findViewById(R.id.btnDoctruyen)).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View view) {
				Intent intent = new Intent(HomeActivity.this, MainActivity.class);
				startActivity(intent);
			}
		});
		
		((Button) findViewById(R.id.btnDownloadApp)).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View view) {
				onDownloadApp();
			}
		});
		
		((Button) findViewById(R.id.btnContact)).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View view) {
				sendContact();
			}
		});
		
		((TextView) findViewById(R.id.tv_contact)).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				sendContact();
			}
		});
		
	}
	
	public void sendContact() {
		PackageInfo pkgInfo = null;
		try {
			pkgInfo = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_META_DATA);
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		String mEmailBody = String.format("\n\n\n\n\n\n" +
				"-------------------" +
				"\nTên ứng dụng: %s\nMã ứng dụng: %s", getString(R.string.app_name), getPackageName());
		if (pkgInfo != null) {
			mEmailBody += String.format("\nMã phiên bản: %d", pkgInfo.versionCode);
		}
		mEmailBody += "\n-------------------";
		
		Intent intent = new Intent(Intent.ACTION_SEND);
		intent.putExtra(Intent.EXTRA_SUBJECT, "Góp ý, nhận xét, yêu cầu truyện");
		intent.putExtra(Intent.EXTRA_TEXT, mEmailBody);
		intent.putExtra(Intent.EXTRA_EMAIL, new String[] { "ungdungviet.llc@gmail.com" });
		intent.setType("message/rfc822");
		try {
			startActivity(Intent.createChooser(intent, "Chọn ứng dụng gửi mail"));
		} catch(ActivityNotFoundException e) {
			Toast.makeText(this, "Không tìm thấy ứng dụng email trong máy", Toast.LENGTH_SHORT).show();
		}
	}
	
	public void onDownloadApp() {
		
		Client client = new Client(getApplicationContext());
		client.setNetworkOption(NetworkOption.OPTION_USE_CACHE);
		client.setEncodingType(DataType.GZIPJSON);
		client.executeAndListener("http://apps.toiyeumobile.org/apps/go_appstore", client.new ClientListener() {

			@Override
			public void onDone(int errorCode, Object data) {
				String url = String.valueOf(data);
				if (url.startsWith("market://")) {
					new UrlHandler(HomeActivity.this).startMarket(url);
				} else if (url.startsWith("http://")) {
					new UrlHandler(HomeActivity.this).startAppStoreActivity(url);
				}
			}
		});
		
	}

	@Override
	public void onBackPressed() {
		
		showDialogExit();
//		super.onBackPressed();
	}
	
}
